﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MulticastTV
{
    public partial class Form1 : Form
    {
        Font OsdFont = new Font("Verdana", 22, FontStyle.Bold);
        Color OsdColor = Properties.Settings.Default.OSDcolor;
        volatile string OsdInput = "";
        string OsdAlign = "";
        int OsdOpacity = 100;
        static int OsdXoffset = 0;
        static int OsdYoffset = 0;

        StringFormat OsdStringFormat;
        StringFormat OsdStringFormat2;
        Timer OsdTimerHide = new Timer();

        public void OsdInit()
        {

            //left
            OsdStringFormat2 = new StringFormat();
            OsdStringFormat2.Alignment = StringAlignment.Near;
            OsdStringFormat2.LineAlignment = StringAlignment.Center;

            //right
            OsdStringFormat = new StringFormat();
            OsdStringFormat.Alignment = StringAlignment.Far;
            OsdStringFormat.LineAlignment = StringAlignment.Center;

            OsdTimerHide.Interval = Properties.Settings.Default.OSDTImeout;
            OsdTimerHide.Tick += new EventHandler(ThideOsd_Tick);
        }


        private void panel5_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

            int width = e.ClipRectangle.Width;
            int height = e.ClipRectangle.Height;
            int test1 = panel5.Width;
            int test2 = panel5.Height;
            if (test1 != 0 && test2 != 0 && width != 0 && height != 0)
            {
                int BlurAmt = 2;
                Rectangle rect = new Rectangle(BlurAmt, BlurAmt, Math.Max(1, width - BlurAmt), Math.Max(1, height - BlurAmt));
                Rectangle rectBlur = new Rectangle(0, 0, width, height);
                if (OsdAlign == "left")
                {
                    //channel ar, crop, rate ...
                    e.Graphics.DrawString(OsdInput, OsdFont, new SolidBrush(OsdColor), rect, OsdStringFormat2);
                }
                else if (OsdAlign == "right")
                //volume
                {
                    e.Graphics.DrawString("llllllllll", OsdFont, Brushes.Gray, rect, OsdStringFormat2);
                    e.Graphics.DrawString(OsdInput, OsdFont, new SolidBrush(OsdColor), rect, OsdStringFormat2);
                }
            }
        }

        /// <summary>
        /// Show OSD
        /// </summary>
        /// <param name="text">Text to show</param>
        /// <param name="edge">Align</param>
        /// <param name="f">Font</param>
        /// <param name="c">Font color</param>
        /// <param name="size">Text size</param>
        /// <param name="alpha">Alpha</param>
        /// <param name="timeout">Timeout</param>
        /// <param name="xofs">X offset</param>
        /// <param name="yxofs">Y offset</param> 
        public void OsdStart(string text, string edge, Font f, Color c, int size, int alpha, int timeout, int xofs, int yofs)
        {
            if (this.Size == new Size(0, 0))
                return;

            OsdInput = text;
            OsdAlign = edge;
            OsdFont = f;
            OsdFont = OsdChangeFontSize(OsdFont, size);
            OsdOpacity = alpha;
            OsdXoffset = xofs;
            OsdYoffset = yofs;
            OsdColor = c;
            OsdTimerHide.Stop();
            if (timeout != 0)
            {
                OsdTimerHide.Interval = timeout;
                OsdTimerHide.Start();
            }

            OsdShowWindow();
        }

        private void OsdShowWindow()
        {
            //if (this.Visible && OsdAlign != "right")
            //    OsdHideWindow();

            int width = this.Width;
            //calculate height from font size
            float ascentpx;
            float descentpx;
            ascentpx = OsdFont.Size * OsdFont.FontFamily.GetCellAscent(OsdFont.Style) / OsdFont.FontFamily.GetEmHeight(OsdFont.Style);
            descentpx = OsdFont.Size * OsdFont.FontFamily.GetCellDescent(OsdFont.Style) / OsdFont.FontFamily.GetEmHeight(OsdFont.Style);

            int height = (int)(Math.Round((descentpx * 3) + ascentpx));

            int stringlength;
            if (OsdAlign == "right")
                stringlength = TextRenderer.MeasureText("llllllllll" + "l", OsdFont).Width;
            else
                stringlength = TextRenderer.MeasureText(OsdInput + "l", OsdFont).Width;

            panel5.Location = new Point(OsdXoffset,OsdYoffset);
            panel5.Size = new Size((int)Math.Max(1, (int)Math.Min(width , stringlength + 2)), (int)Math.Max(1, height));

            panel5.Invalidate(panel5.ClientRectangle);
            panel5.Update();
            
            if (!panel5.Visible)
            {
                panel5.Visible = true;
                panel5.BringToFront();
            }

            //if (panel5.Focused)
            //    pictureBox1.Focus();
        }

        public void OsdHideWindow()
        {
            panel5.Visible = false;
        }

        private void ThideOsd_Tick(object sender, EventArgs e)
        {
            OsdHideWindow();
            OsdTimerHide.Stop();
        }

        static public Font OsdChangeFontSize(Font font, float fontSize)
        {
            if (font != null)
            {
                float currentSize = font.Size;
                if (currentSize != fontSize)
                {
                    font = new Font(font.Name, fontSize,
                        font.Style, font.Unit,
                        font.GdiCharSet, font.GdiVerticalFont);
                }
            }
            return font;
        }

        public string OsdGetText()
        {
            return OsdInput;
        }
    }
}
